/**
 * 安卓白金英雄坛制作组
 * <br>
 * 文件名：StuntData.java
 * <br>
 * 创建时间：2015-3-28 下午12:25:12
 * <br>
 * 所属项目：GmudEX
 * <br>
 *
 * @author 12548
 *         <br>
 */
package lostland.gmud.exv2.battle;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.blgframework.impl.BLGFileIO;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;

/**
 * 类名：StuntData
 * <br>
 * 说明：绝招数据
 *
 * @author 12548
 */
public class StuntData {

	private static ArrayList<StuntData> _instance;

	public static StuntData getInstance(int id) {
		if (_instance == null) {
			init();
		}
		return _instance.get(id);
	}

	public static ArrayList<StuntData> getInstance() {
		if (_instance == null) {
			init();
		}
		return _instance;
	}

	/**
	 * @param name
	 * @param cd
	 * @param cost
	 * @param weaponType
	 * @param skillNeeded
	 */
	public StuntData(String name, int cd, int cost, int weaponType,
	                 ArrayList<SkillNeeded> skillNeeded) {
		super();
		this.name = name;
		this.cd = cd;
		this.cost = cost;
		this.weaponType = weaponType;
		this.skillNeeded = skillNeeded;
	}

	public int id;
	public String name;
	public int cd;
	public int cost;
	public int weaponType;
	public ArrayList<SkillNeeded> skillNeeded;
	public int equipSkillNeeded[] = {-1, -1, -1, -1, -1};
	public int talentNeeded = -1;

	public static class SkillNeeded {
		/**
		 * @param skillid
		 * @param skilllv
		 */
		public SkillNeeded(int skillid, int skilllv) {
			super();
			this.skillid = skillid;
			this.skilllv = skilllv;
		}

		public int skillid;
		public int skilllv;
	}


	public String canUse(Npc npc) {
		if(!npc.isStuntAvaliable())
			return "你当前不能使用绝招";
		if (npc.getGauge() < GameConstants.STUNT_GAUGE_COST)
			return "你的怒气不足";
		if (npc.getSz(id) > 0)
			return "你刚用完外功，还是先歇歇吧";
		if (npc.getFp() < cost)
			return "你的内力不足。";
		if (weaponType >= 0 && npc.getAttackItem().getSubkind() != weaponType)
			return "趁手的家伙都没有一把，瞎比划什么";
		if (talentNeeded > -1 && !npc.haveTalent(talentNeeded))
			return "你没有所需天赋！";
		if (id == 7 && npc.getAttr(Room.ATTR_STR) < 32)
			return "你膂力不足，不能使用流星飞掷！";

		for (int i = 0; i < 5; i++) {
			if (equipSkillNeeded[i] > -1) {
				if (npc.skillsckd[i] != equipSkillNeeded[i]) {
					Logger.d(npc.skillsckd[i] + "!=" + equipSkillNeeded[i] + "on" + i);
					return "你未装备所需技能！";
				}
			}
		}

		for (SkillNeeded sn : skillNeeded) {
			if (npc.skills[sn.skillid] < sn.skilllv)
				return "你的" + Gmud.skill[sn.skillid].name + "修为不足！";
		}

		return null;
	}

	public static StuntData find(String name) {
		for (StuntData sd : _instance) {
			if (sd.name.equals(name)) return sd;
		}
		return null;
	}


	/**
	 * 绝招列表
	 */
	public static final String NAME[] = new String[]{"震字诀", "挤字诀", "阴阳诀", "乱环诀", "缠字诀", "连字诀",
			"三环套月", "流星飞掷",
			"雪花六出", "神倒鬼跌", "落英缤纷", "柳浪闻莺", "化掌为刀", "八卦刀影掌", "八阵刀影掌", "化掌为刀", "旋风三连斩", "迎风一刀斩",
			"雷动九天", "红莲出世", "冰心诀",
			"三花", "忍术烟幕", "忍法影分身", "乾坤一掷", "飞龙探云手", "恶虎啸", "飞鹰召唤", "闪展腾挪"};

	/**
	 * 发动绝招所需要装备的武器的类别(subkind)<br>
	 * 1刀 6剑 7杖 9鞭 11拳脚
	 */
	public static final int weaponskNeeded[] = new int[]{11, 11, 11, 11, 6, 6, 6, 7, 6, 11, 9, 1,
			11, 1, 1, 11, 1, 1,
			11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11};

	/**
	 * 发动绝招所需要的技能和等级
	 */
	public static final int sklNeeded[][] = new int[][]{{1, 31, 0, 32},
			{60, 60, 60, 60},// 震字诀
			{1, 31, 0, 32},
			{70, 70, 70, 70},// 挤字诀
			{1, 31, 0, 32},
			{120, 120, 120, 120},// 阴阳
			{1, 31, 0, 32},
			{100, 100, 100, 100},// 乱环
			{2, 30, 0, 32},
			{80, 80, 80, 80},// 缠字决
			{2, 30, 0, 32},
			{80, 80, 80, 80},// 连字诀
			{2, 30, 0, 32},
			{120, 120, 120, 120},// 三环套月
			{0, 25, 5, 23},
			{80, 80, 80, 80},// 流星飞掷
			{2, 38, 0, 36},
			{60, 60, 60, 60},// 雪花六出
			{1, 39, 0, 36},
			{80, 80, 100, 100},// 神倒鬼跌
			{6, 17, 0, 20},
			{80, 80, 80, 80},// 落英缤纷
			{1, 3, 18, 0, 20},
			{20, 100, 100, 100, 100},// 柳浪闻莺
			{12, 1, 0, 14},
			{70, 70, 70, 70},// 化掌为刀
			{11, 0, 14}, {90, 70, 70}, {11, 0, 14}, {90, 70, 70}, {0, 1, 13},
			{90, 90, 90},// 化掌为刀
			{0, 26, 3, 29}, {80, 80, 60, 60}, {0, 26, 3, 29}, {80, 80, 80, 80}, {0, 25},
			{60, 60},// 雷动九天
			{0, 25}, {80, 80}, {0, 36}, {60, 60}, {0, 20}, {60, 60}, {0, 26},
			{60, 60},// 忍术烟幕
			{0, 26}, {80, 80}, {0}, {0}, {0}, {0}, {1, 40, 0}, {80, 100, 120},
			{1, 47, 0},
			{80, 100, 100}, {0, 50}, {20, 40}};

	/**
	 * 发动一次绝招所耗费的内力
	 */
	public static final int COST[] = new int[]{200, 350, 300, 500, 250, 350, 400, 850, 600, 700,
			400, 400, 200, 450,
			150, 400, 350, 550, 150, 350, 250, 350, 300, 550, 0, 100, 300, 200, 150};

	/**
	 * 收招回合（发动一次后的间隔时间）
	 */
	public static final int CD[] = new int[]{2, 2, 6, 9, 6, 9, 5, 5, 4, 9, 4, 4, 4, 4, 4, 4, 7,
			7, 5, 5, 5, 5, 5, 5,
			5, 1, 6, 5, 2};

	private static void init() {

		BLGFileIO f = (BLGFileIO) Game.getInstance().getFileIO();
		BufferedReader in = null;

		try {
			in = new BufferedReader(new InputStreamReader(f.readAsset("data/StuntData.dat")));
			String s = "";

			s = in.readLine();
			Gson gson = new Gson();
			_instance = gson.fromJson(s, new TypeToken<ArrayList<StuntData>>() {
			}.getType());

		} catch (IOException e) {
			e.printStackTrace();
		} catch (JsonSyntaxException e2) {
			e2.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}

	}
}
